﻿
using System;
using System.Web;
using System.Web.Caching;

using ns_User;
using ns_MsgRecords;
using ns_SessionManage;


namespace ns_CacheManage
{

    public static class c_CacheManage
    {
        private const String EMPTY_FLAG = "Empty";
        private const Int32 TIME_OUT = 15; //cache过期时间，单位为秒
        private static Boolean Put(String keyword, Object obj)
        {
            try
            {
                if (obj == null) return false;
                HttpContext.Current.Cache.Insert(keyword, obj, null, DateTime.Now.AddSeconds(15), TimeSpan.Zero);
                return true;
            }
            catch(Exception e ) 
            {
                return false;
            }
            }
        private static T Get<T>(String keyword)
        {
            return (T)HttpContext.Current.Cache[keyword];
        }
        private static Boolean Del(String keyword)
        {
            try
            {
                HttpContext.Current.Cache.Remove(keyword);
                return true;
            }
            catch (Exception e)
            { return false; }
        }
        public static Boolean DelMsgCahce(String name)
        {
            if (Del(name)) return true;
            else return false;
        }
        public static Boolean ClearMsgCache(String name)
        {
            if (IsCacheExist(name))
            {
                Put(name, EMPTY_FLAG); //更新记录
                return true;
            }
            return false;
        }
        public static Boolean NewMsgCache(String name)
        {
            try
            {
                Put(name, EMPTY_FLAG);
                return true;
            }
            catch (Exception e)
            { return false; }
        }
        public static Boolean IsCacheExist(String keyword)
        {
            if (Get<Object>(keyword) != null)
            {
               object obj = Get<Object>(keyword);
                return true;
            }
            else return false;
        }
        public static String GetMsgCache(String name)
        {
            if (IsCacheExist(name))
            {
                String cache = Get<String>(name);
                HttpContext.Current.Cache.Remove(name);//删除
                Put(name, EMPTY_FLAG); //更新记录
                if (cache == EMPTY_FLAG) return null;
                return cache;
            }
            else
            {
                NewMsgCache(name);//被意外删除或过期删除，重建
                return null;
            }
        }
        public static Boolean SendMsg(String toName, String content)
        {
            if (content == null && IsCacheExist(toName)) return false;
            if (Get<String>(toName) != EMPTY_FLAG)
            { 
                c_MsgRecordsXml mrx = new c_MsgRecordsXml(Get<String>(toName),true);
                content = mrx.AddMsgRecords(new c_MsgRecordsXml(content,true));
            }
            Put(toName, content);
            return true;
        }       
    }
}